package com.dream.dreamhouse.controller;

import com.dream.dreamhouse.common.utils.IpUtils;
import com.dream.dreamhouse.entity.Dream;
import com.dream.dreamhouse.entity.vo.DreamVo;
import com.dream.dreamhouse.service.DreamService;
import com.dream.dreamhouse.common.entity.AjaxResult;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lizhen
 * @date 2025/6/6 17:03
 */
@RestController
@RequestMapping("/dream")
@Slf4j
public class DreamController {

    @Resource
    private DreamService dreamService;

    /**
     * 获取梦想分页列表数据
     *
     * @param dreamVo
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult getDreamList(@RequestBody DreamVo dreamVo) {
        try {
            PageInfo<Dream> dreamPageInfo = dreamService.getDreamPage(dreamVo);
            return AjaxResult.success("查询成功！", dreamPageInfo);
        } catch (Exception e) {
            log.error("查询失败{}", e.getMessage(), e);
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 新增梦想数据
     *
     * @param dreamVo
     * @return
     */
    @PostMapping("/addDream")
    public AjaxResult addDream(@RequestBody DreamVo dreamVo, HttpServletRequest request) {
        try {
            String ipAddress = IpUtils.getIpAddress(request);
            dreamVo.setIp(ipAddress);
            dreamService.addDream(dreamVo);
            return AjaxResult.success("新增成功！");
        } catch (Exception e) {
            log.error("新增失败{}", e.getMessage(), e);
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 根据点赞数降序查询
     *
     * @return
     */
    @PostMapping("/findByLikes")
    public AjaxResult findByLikes(@RequestBody DreamVo dreamVo) {
        try {
            PageInfo<Dream> dreamList = dreamService.findByLikes(dreamVo);
            return AjaxResult.success("查询成功！", dreamList);
        } catch (Exception e) {
            log.error("查询失败{}", e.getMessage(), e);
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 点赞
     *
     * @return
     */
    @PostMapping("/increaseLikeCount")
    public AjaxResult increaseLikeCount(@RequestBody DreamVo dreamVo) {
        try {
            Preconditions.checkArgument(StringUtils.isNotEmpty(dreamVo.getId()), "梦想id为空！");
            dreamService.increaseLikeCount(dreamVo);
            return AjaxResult.success("点赞成功！");
        } catch (Exception e) {
            log.error("点赞失败{}", e.getMessage(), e);
            return AjaxResult.error("点赞失败");
        }
    }

    /**
     * 取消点赞
     *
     * @param dreamVo
     * @return
     */
    @PostMapping("/decreaseLikeCount")
    public AjaxResult decreaseLikeCount(@RequestBody DreamVo dreamVo) {
        try {
            Preconditions.checkArgument(StringUtils.isNotEmpty(dreamVo.getId()), "梦想id为空！");
            dreamService.decreaseLikeCount(dreamVo);
            return AjaxResult.success("取消点赞成功！");
        } catch (Exception e) {
            log.error("取消点赞失败{}", e.getMessage(), e);
            return AjaxResult.error("取消点赞失败");
        }
    }

    /**
     * 根据时间排序查询
     *
     * @return
     */
    @PostMapping("/findByTime")
    public AjaxResult findByTime(@RequestBody DreamVo dreamVo) {
        try {
            PageInfo<Dream> dreamList = dreamService.findByTime(dreamVo);
            return AjaxResult.success("查询成功！", dreamList);
        } catch (Exception e) {
            log.error("查询失败{}", e.getMessage(), e);
            return AjaxResult.error("查询失败");
        }
    }
}
